package com.cinema.model;

import java.util.HashMap;
import java.util.Map;

public class CinemaHall {
    private String hallId;
    private String hallName;
    private int rows;
    private int seatsPerRow;
    private Map<String, Boolean> seats; // 座位状态映射

    public CinemaHall(String hallId, String hallName, int rows, int seatsPerRow) {
        this.hallId = hallId;
        this.hallName = hallName;
        this.rows = rows;
        this.seatsPerRow = seatsPerRow;
        initializeSeats();
    }

    private void initializeSeats() {
        seats = new HashMap<>();
        for (int row = 1; row <= rows; row++) {
            for (int seat = 1; seat <= seatsPerRow; seat++) {
                String seatNumber = row + "-" + seat;
                seats.put(seatNumber, false); // false表示空闲
            }
        }
    }

    // Getters
    public String getHallId() { return hallId; }
    public String getHallName() { return hallName; }
    public int getRows() { return rows; }
    public int getSeatsPerRow() { return seatsPerRow; }
    public int getTotalSeats() { return rows * seatsPerRow; }
    public Map<String, Boolean> getSeats() { return seats; }

    public int getAvailableSeatsCount() {
        return (int) seats.values().stream().filter(occupied -> !occupied).count();
    }

    public boolean isSeatAvailable(String seatNumber) {
        return seats.getOrDefault(seatNumber, false);
    }

    public boolean occupySeat(String seatNumber) {
        if (seats.containsKey(seatNumber) && !seats.get(seatNumber)) {
            seats.put(seatNumber, true);
            return true;
        }
        return false;
    }

    public boolean releaseSeat(String seatNumber) {
        if (seats.containsKey(seatNumber)) {
            seats.put(seatNumber, false);
            return true;
        }
        return false;
    }

    public void displaySeatMap() {
        System.out.println("\n=== " + hallName + " 座位图 ===");
        System.out.print("   ");
        for (int i = 1; i <= seatsPerRow; i++) {
            System.out.printf("%2d ", i);
        }
        System.out.println();

        for (int row = 1; row <= rows; row++) {
            System.out.printf("%2d ", row);
            for (int seat = 1; seat <= seatsPerRow; seat++) {
                String seatNumber = row + "-" + seat;
                String status = seats.get(seatNumber) ? "❌" : "✅";
                System.out.print(status + " ");
            }
            System.out.println();
        }
        System.out.println("✅: 空闲  ❌: 已售");
    }
}
